package cn.com.mr01.lianxi.mr_lianxi_05;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * 1. 黑名单内容在Driver中读取一次,将黑名单内容设置到Configuration对象中
 * 2.context.getConfiguration()传输到map中，设置给blackInfo
 */
public class TokenizerMapper extends
		Mapper<LongWritable, Text, Text, IntWritable> {
	private Set<String> blackInfo = new HashSet<String>();

	IntWritable n = new IntWritable(1);
	Text k = new Text();

	@Override
	protected void setup(Context context) throws IOException,
			InterruptedException {
		// 生成黑名单
		String[] blacks = context.getConfiguration().getStrings("blackInfo");
		blackInfo = new HashSet<String>(Arrays.asList(blacks));
	}

	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {// 1. 切分
		// 1. 切分
		String[] words = value.toString().split(",");
		for (String word : words) {
			if (!blackInfo.contains(word)) {
				k.set(word);
				context.write(k, n);
			}
		}
	}
}
